package com.example.allshengmedical.experimentanalyze.activity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.IInterface;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseApplication;
import com.example.allshengmedical.base.BaseFragment;
import com.example.allshengmedical.experimentanalyze.bean.CtSettingBean;
import com.example.allshengmedical.experimentnew.bean.LayoutTableBean;
import com.example.allshengmedical.experimentnew.bean.MethodBean;
import com.example.allshengmedical.system.activity.NumberFormat3;
import com.example.allshengmedical.util.ByAlert;
import com.example.allshengmedical.util.SharedPreferencesUtils;


//参数设置中的ct设置界面
public class CtFragment extends BaseFragment {
	private ListView mylist;
	private CheckBox default_check, auto_threshold_check, auto_baseline_check;
	private EditText start_edit, end_edit;
	private TextView start_add, start_reduce, end_add, end_reduce, edit_pop_tv, confirmBtn;
	private MyCtAdapter adapter;
	TextView Threshold_tv;
	CheckBox pop_threshold_check;
	CheckBox pop_baseline_check;
	TextView pop_Threshold_tv, pop_start_add, pop_start_reduce, pop_end_add, pop_end_reduce;
	TextView pop_start_edit, pop_end_edit, fra_confirmBtn;
	EditText pop_Threshold_edit, Threshold_edit;
	RelativeLayout pop_rel;
	RelativeLayout default_rel;

	private String defaultThreshold = "0.2";
	private String defaultStart = "3";
	private String defaultEnd = "15";
	public Boolean defaultThresholdBoo = true;
	public Boolean defaultBaselineBoo = true;
	LinearLayout pop_baseline_start_lin;
	LinearLayout baseline_start_lin;
	// 基因盘数据
	public ArrayList<LayoutTableBean> activityLayoutTableBeans;
	// 数据
	String folderName;
	String fileName;
	private String appFilePath = Environment.getExternalStorageDirectory() + "/" + "AllShengMedical";
	StringBuffer contextString = new StringBuffer();

	public ArrayList<CtSettingBean> ctSettingBeans = new ArrayList<CtSettingBean>();

	public static CtFragment ctFragment;

	// 接口huidiao
	CallBackValue callBackValue;

	// 曲线图
	// 上半部
	private ArrayList<MethodBean> activityMethodBeans = new ArrayList<MethodBean>();
	// 重复次数
	private int repititions;

	/**
	 * fragment与activity产生关联是 回调这个方法
	 */
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		// 当前fragment从activity重写了回调接口 得到接口的实例化对象
		callBackValue = (CallBackValue) getActivity();
	}

	public static CtFragment getCtFragment() {
		return ctFragment;
	}

	@Override
	public int getlayout() {
		return R.layout.fragment_ct;
	}

	@Override
	protected void initView() {

		ctFragment = this;
		getInfo();
		getDefaultValue();

		adapter = new MyCtAdapter(ctSettingBeans);
		mylist.setAdapter(adapter);
		final NumberFormat3 numberFormat3 = new NumberFormat3();
		numberFormat3.Format(Threshold_edit);

		default_check.setChecked(ctSettingBeans.get(0).isUseDaultSetting());
		auto_threshold_check.setChecked(ctSettingBeans.get(0).isAutoThreshold());
		Threshold_edit.setText(ctSettingBeans.get(0).getThreshold());
		auto_baseline_check.setChecked(ctSettingBeans.get(0).isAutoBaseLine());
		start_edit.setText(ctSettingBeans.get(0).getBaselineStart());
		end_edit.setText(ctSettingBeans.get(0).getBaselineEnd());

		fra_confirmBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (Integer.parseInt(start_edit.getText().toString()) > Integer
						.parseInt(end_edit.getText().toString())) {

					ByAlert.alert(getActivity().getString(R.string.ctfragment_startend));
					return;
				}
				for (int i = 0; i < ctSettingBeans.size(); i++) {

					if (ctSettingBeans.get(i).isSelected()) {
						ctSettingBeans.get(i).setUseDaultSetting(default_check.isChecked());
						ctSettingBeans.get(i).setAutoThreshold(auto_threshold_check.isChecked());
						ctSettingBeans.get(i).setThreshold(Threshold_edit.getText().toString().trim());
						ctSettingBeans.get(i).setAutoBaseLine(auto_baseline_check.isChecked());
						ctSettingBeans.get(i).setBaselineStart(start_edit.getText().toString().trim());
						ctSettingBeans.get(i).setBaselineEnd(end_edit.getText().toString().trim());
						adapter.notifyDataSetChanged();
						// 给activity发送传值
						callBackValue.SendMessageValue(ctSettingBeans, defaultBaselineBoo, defaultStart, defaultEnd,
								defaultThresholdBoo, defaultThreshold);
						break;
					}
				}

				ByAlert.alert("信息确认完成");
			}
		});

		default_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// checked(isChecked);
				if (isChecked) {
					setNoFocus(auto_threshold_check);
					setNoFocus(baseline_start_lin);
					setNoFocus(Threshold_tv);
					setNoFocus(Threshold_edit);
					setNoFocus(auto_baseline_check);
					auto_threshold_check.setChecked(true);
					auto_baseline_check.setChecked(true);
				} else {
					setOnFocus(auto_threshold_check);
					setNoFocus(baseline_start_lin);
					setNoFocus(Threshold_tv);
					setNoFocus(Threshold_edit);
					setNoFocus(auto_baseline_check);

				}
			}
		});
		auto_threshold_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					setNoFocus(Threshold_tv);
					setNoFocus(Threshold_edit);
					setNoFocus(auto_baseline_check);
					setNoFocus(baseline_start_lin);
					auto_baseline_check.setChecked(true);
				} else {
					setOnFocus(Threshold_tv);
					setOnFocus(Threshold_edit);
					setOnFocus(auto_baseline_check);
					setNoFocus(baseline_start_lin);

				}
			}
		});
		auto_baseline_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				for (int i = 0; i < ctSettingBeans.size(); i++) {
					if (isChecked) {
						setNoFocus(baseline_start_lin);
						setNoFocus(start_reduce);
						setNoFocus(start_edit);
						setNoFocus(start_add);
						setNoFocus(end_reduce);
						setNoFocus(end_edit);
						setNoFocus(end_add);
					} else {
						setOnFocus(baseline_start_lin);
						setOnFocus(start_reduce);
						setOnFocus(start_edit);
						setOnFocus(start_add);
						setOnFocus(end_reduce);
						setOnFocus(end_edit);
						setOnFocus(end_add);
					}
				}
			}
		});
		onClick();
		edit_pop_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				View view = LayoutInflater.from(getActivity()).inflate(R.layout.pop_ct_fragment, null);
				confirmBtn = (TextView) view.findViewById(R.id.confirmBtn);
				pop_rel = (RelativeLayout) view.findViewById(R.id.pop_rel);
				pop_threshold_check = (CheckBox) view.findViewById(R.id.pop_threshold_check);
				pop_baseline_check = (CheckBox) view.findViewById(R.id.pop_baseline_check);
				pop_Threshold_tv = (TextView) view.findViewById(R.id.pop_Threshold_tv);
				pop_start_add = (TextView) view.findViewById(R.id.pop_start_add);
				pop_start_reduce = (TextView) view.findViewById(R.id.pop_start_reduce);
				pop_end_add = (TextView) view.findViewById(R.id.pop_end_add);
				pop_end_reduce = (TextView) view.findViewById(R.id.pop_end_reduce);
				pop_Threshold_edit = (EditText) view.findViewById(R.id.pop_Threshold_edit);
				pop_start_edit = (TextView) view.findViewById(R.id.pop_start_edit);
				pop_end_edit = (TextView) view.findViewById(R.id.pop_end_edit);
				pop_baseline_start_lin = (LinearLayout) view.findViewById(R.id.pop_baseline_start_lin);

				pop_Threshold_edit.setText(defaultThreshold);
				pop_start_edit.setText(defaultStart);
				pop_end_edit.setText(defaultEnd);
				numberFormat3.Format(pop_Threshold_edit);
				if (defaultThresholdBoo) {
					pop_threshold_check.setChecked(true);
				} else {
					pop_threshold_check.setChecked(false);
				}

				if (defaultBaselineBoo) {
					pop_baseline_check.setChecked(true);
				} else {
					pop_baseline_check.setChecked(false);
				}
				if (pop_threshold_check.isChecked()) {
					setNoFocus(pop_Threshold_tv);
					setNoFocus(pop_Threshold_edit);
					setNoFocus(pop_baseline_check);
					setNoFocus(pop_baseline_start_lin);
					setNoFocus(pop_start_add);
					setNoFocus(pop_start_edit);
					setNoFocus(pop_start_reduce);
					setNoFocus(pop_end_add);
					setNoFocus(pop_end_edit);
					setNoFocus(pop_end_reduce);
					pop_baseline_check.setChecked(true);
				} else {
					setOnFocus(pop_Threshold_tv);
					setOnFocus(pop_Threshold_edit);
					setOnFocus(pop_baseline_check);
					setNoFocus(pop_baseline_start_lin);
					setNoFocus(pop_start_add);
					setNoFocus(pop_start_edit);
					setNoFocus(pop_start_reduce);
					setNoFocus(pop_end_add);
					setNoFocus(pop_end_edit);
					setNoFocus(pop_end_reduce);
				}
				if (pop_baseline_check.isChecked()) {
					setNoFocus(pop_baseline_start_lin);
					setNoFocus(pop_start_add);
					setNoFocus(pop_start_edit);
					setNoFocus(pop_start_reduce);
					setNoFocus(pop_end_add);
					setNoFocus(pop_end_edit);
					setNoFocus(pop_end_reduce);
				} else {
					setOnFocus(pop_Threshold_tv);
					setOnFocus(pop_Threshold_edit);
					setOnFocus(pop_baseline_check);
					setOnFocus(pop_baseline_start_lin);
					setOnFocus(pop_start_add);
					setOnFocus(pop_start_edit);
					setOnFocus(pop_start_reduce);
					setOnFocus(pop_end_add);
					setOnFocus(pop_end_edit);
					setOnFocus(pop_end_reduce);
				}
				pop_threshold_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							setNoFocus(pop_Threshold_tv);
							setNoFocus(pop_Threshold_edit);
							setNoFocus(pop_baseline_check);
							setNoFocus(pop_baseline_start_lin);
							setNoFocus(pop_start_add);
							setNoFocus(pop_start_edit);
							setNoFocus(pop_start_reduce);
							setNoFocus(pop_end_add);
							setNoFocus(pop_end_edit);
							setNoFocus(pop_end_reduce);
							pop_baseline_check.setChecked(true);
						} else {
							setOnFocus(pop_Threshold_tv);
							setOnFocus(pop_Threshold_edit);
							setOnFocus(pop_baseline_check);
							setNoFocus(pop_baseline_start_lin);
							setNoFocus(pop_start_add);
							setNoFocus(pop_start_edit);
							setNoFocus(pop_start_reduce);
							setNoFocus(pop_end_add);
							setNoFocus(pop_end_edit);
							setNoFocus(pop_end_reduce);
						}
					}
				});
				pop_baseline_check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						// TODO Auto-generated method stub
						if (isChecked) {
							setNoFocus(pop_baseline_start_lin);
							setNoFocus(pop_start_add);
							setNoFocus(pop_start_edit);
							setNoFocus(pop_start_reduce);
							setNoFocus(pop_end_add);
							setNoFocus(pop_end_edit);
							setNoFocus(pop_end_reduce);
						} else {
							setOnFocus(pop_Threshold_tv);
							setOnFocus(pop_Threshold_edit);
							setOnFocus(pop_baseline_check);
							setOnFocus(pop_baseline_start_lin);
							setOnFocus(pop_start_add);
							setOnFocus(pop_start_edit);
							setOnFocus(pop_start_reduce);
							setOnFocus(pop_end_add);
							setOnFocus(pop_end_edit);
							setOnFocus(pop_end_reduce);
						}
					}
				});
				popOnClick();

				final PopupWindow popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT);
				confirmBtn.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// pop_Threshold_edit.setText(defaultThreshold);
						// pop_start_edit.setText(defaultStart);
						// pop_end_edit.setText(defaultEnd);、
						defaultThreshold = pop_Threshold_edit.getText() + "";
						defaultStart = pop_start_edit.getText() + "";
						defaultEnd = pop_end_edit.getText() + "";
						defaultThresholdBoo = pop_threshold_check.isChecked();
						defaultBaselineBoo = pop_baseline_check.isChecked();
						
						if (Integer.parseInt(pop_start_edit.getText().toString()) > Integer
								.parseInt(pop_end_edit.getText().toString())) {
							
							ByAlert.alert(getActivity().getString(R.string.ctfragment_startend));
							return;
						}

						// ctSettingBeans.clear();
						for (int i = 0; i < activityLayoutTableBeans.size(); i++) {
							// if (defaultThresholdBoo) {
							// ctSettingBeans.add(new CtSettingBean(
							// activityLayoutTableBeans.get(i)
							// .getTarget(), "AUTO", "AUTO",
							// "AUTO"));
							// } else {
							// if (defaultBaselineBoo) {
							// ctSettingBeans.add(new CtSettingBean(
							// activityLayoutTableBeans.get(i)
							// .getTarget(),
							// defaultThreshold, "AUTO", "AUTO"));
							// } else {
							// ctSettingBeans.add(new CtSettingBean(
							// activityLayoutTableBeans.get(i)
							// .getTarget(),
							// defaultThreshold, defaultStart,
							// defaultEnd));
							// }
							// }
							if (ctSettingBeans.get(i).isUseDaultSetting()) {
								// ctSettingBeans
								// .add(new CtSettingBean(
								// activityLayoutTableBeans.get(i)
								// .getTarget(),
								// defaultThreshold, defaultStart,
								// defaultEnd));
								ctSettingBeans.get(i).setThreshold(defaultThreshold);
								ctSettingBeans.get(i).setBaselineStart(defaultStart);
								ctSettingBeans.get(i).setBaselineEnd(defaultEnd);
							} else {

							}

						}
						ctSettingBeans.get(0).setSelected(true);
						adapter.notifyDataSetChanged();
						// 给activity发送传值
						callBackValue.SendMessageValue(ctSettingBeans, defaultBaselineBoo, defaultStart, defaultEnd,
								defaultThresholdBoo, defaultThreshold);
						popupWindow.dismiss();

					}
				});

				popupWindow.setBackgroundDrawable(new BitmapDrawable());
				popupWindow.setFocusable(true);
				popupWindow.setOutsideTouchable(true);
				popupWindow.showAsDropDown(edit_pop_tv, 0, 2);

			}
		});

		// 给activity发送传值
		callBackValue.SendMessageValue(ctSettingBeans, defaultBaselineBoo, defaultStart, defaultEnd,
				defaultThresholdBoo, defaultThreshold);

		// 进页面得默认灰白状态
		if (default_check.isChecked()) {
			setNoFocus(auto_threshold_check);
			setNoFocus(baseline_start_lin);
			setNoFocus(Threshold_tv);
			setNoFocus(Threshold_edit);
			setNoFocus(auto_baseline_check);
			auto_threshold_check.setChecked(true);
			auto_baseline_check.setChecked(true);
		} else {
			setOnFocus(auto_threshold_check);
			setNoFocus(baseline_start_lin);
			setNoFocus(Threshold_tv);
			setNoFocus(Threshold_edit);
			setNoFocus(auto_baseline_check);
		}
		if (auto_threshold_check.isChecked()) {
			setNoFocus(Threshold_tv);
			setNoFocus(Threshold_edit);
			setNoFocus(auto_baseline_check);
			setNoFocus(baseline_start_lin);
			auto_baseline_check.setChecked(true);
		} else {
			setOnFocus(Threshold_tv);
			setOnFocus(Threshold_edit);
			setOnFocus(auto_baseline_check);
			setNoFocus(baseline_start_lin);
		}
		if (auto_baseline_check.isChecked()) {
			setNoFocus(baseline_start_lin);
			setNoFocus(start_reduce);
			setNoFocus(start_edit);
			setNoFocus(start_add);
			setNoFocus(end_reduce);
			setNoFocus(end_edit);
			setNoFocus(end_add);
		} else {
			setOnFocus(baseline_start_lin);
			setOnFocus(start_reduce);
			setOnFocus(start_edit);
			setOnFocus(start_add);
			setOnFocus(end_reduce);
			setOnFocus(end_edit);
			setOnFocus(end_add);
		}
	};

	private void getInfo() {
		contextString = new StringBuffer();
		folderName = SharedPreferencesUtils.get(getActivity(), BaseApplication.FILEMANGER_PREPAREFOLDERPATH, "") + "";
		fileName = SharedPreferencesUtils.get(getActivity(), BaseApplication.FILEMANGER_PREPAREFILENAME, "") + "";

		String path = appFilePath + "/" + folderName;

		File file = new File(path + "/" + fileName);
		BufferedReader reader = null;
		contextString=new StringBuffer();
		System.gc();
		try {
			System.out.println("以行为单位读取文件内容，一次读一整行:");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				// System.out.println("line " + line + ": " + tempString);
				// Log.e("--", tempString);
				contextString.append(tempString);
				contextString.append(",");
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
					// LogUtil.e("!!", contextString + "");

				} catch (IOException e1) {
				}
			}
		}

		// layoutOne的数据解析
		activityLayoutTableBeans = new ArrayList<LayoutTableBean>();

		// LogUtil.e(contextString.indexOf(",Aisle") + 7);
		// LogUtil.e(contextString.indexOf(",Color"));

		int index = 0;
		while (!(contextString.indexOf("Aisle", index) <= 0)) {

			// Aisle:
			String aisle;
			aisle = contextString.substring(contextString.indexOf(",Aisle", index) + 7,

					contextString.indexOf(",Color", index));
					// LogUtil.e(aisle);

			// Color
			String color;
			color = contextString.substring(contextString.indexOf(",Color", index) + 7,

					contextString.indexOf(",Target", index));
					// LogUtil.e(color);

			// Target
			String target;
			target = contextString.substring(contextString.indexOf(",Target", index) + 8,

					contextString.indexOf(",Dyes", index));

			// LogUtil.e(target);

			int nextIndex = contextString.indexOf(",Aisle", index) + 7;

			// Dyes
			String dyes;
			if (contextString.indexOf(",Aisle", nextIndex) < 0) {

				dyes = contextString.substring(contextString.indexOf(",Dyes", index) + 6,
						contextString.indexOf(",,dataSourceListBlock1", index));

				// LogUtil.e(dyes);
			} else {

				dyes = contextString.substring(contextString.indexOf(",Dyes", index) + 6,

						contextString.indexOf(",Aisle", nextIndex));
				// LogUtil.e(dyes);
			}

			index = contextString.indexOf(",Dyes", index) + 5;

			activityLayoutTableBeans.add(new LayoutTableBean(false, Integer.parseInt(aisle.trim()),
					Integer.parseInt(color.trim()), target, dyes));

		}
		int index1 = 0;
		// 上面解析
		while (!(contextString.indexOf("steptType:", index1) <= 0)) {

			// steptType:
			String steptType;
			steptType = contextString.substring(contextString.indexOf("steptType:", index1) + 10,

					contextString.indexOf(",temperature:", index1));

			// LogUtil.e(steptType);

			// temperature
			String temperature;
			temperature = contextString.substring(contextString.indexOf(",temperature:", index1) + 13,

					contextString.indexOf(",time:", index1));

			// LogUtil.e(temperature);
			// time
			String time;
			time = contextString.substring(contextString.indexOf(",time:", index1) + 6,

					contextString.indexOf(",goTo:", index1));
					// LogUtil.e(time);

			// goTo
			String goTo;
			goTo = contextString.substring(contextString.indexOf(",goTo:", index1) + 6,

					contextString.indexOf(",Repititions:", index1));
			// LogUtil.e(goTo);
			// Repititions
			String repititions;
			repititions = contextString.substring(contextString.indexOf(",Repititions:", index1) + 13,

					contextString.indexOf(",deltaT:", index1));
			// LogUtil.e(repititions);
			// deltaT
			String deltaT;
			deltaT = contextString.substring(contextString.indexOf(",deltaT:", index1) + 8,

					contextString.indexOf(",rightSelect:", index1));
			// LogUtil.e(deltaT);

			int nextIndex = contextString.indexOf("steptType:", index1) + 10;

			// rightSelect
			String rightSelect;

			if (contextString.indexOf(",steptType:", nextIndex) < 0) {
				rightSelect = contextString.substring(contextString.indexOf(",rightSelect:", index1) + 13,
						contextString.indexOf(",,pointMap", index1));
				if ("null".equals(rightSelect)) {
					rightSelect = "";
				}

			} else {
				rightSelect = contextString.substring(contextString.indexOf(",rightSelect:", index1) + 13,

						contextString.indexOf(",steptType:", nextIndex));
				if ("null".equals(rightSelect)) {
					rightSelect = "";
				}
			}
			// LogUtil.e(rightSelect);
			index1 = contextString.indexOf(",rightSelect:", index1) + 13;

			activityMethodBeans.add(new MethodBean(steptType.trim(), temperature.trim(), time.trim(), goTo.trim(),
					repititions.trim(), deltaT.trim()));
		}
		// 获取重复次数

		int stepNum = Integer.parseInt(contextString
				.substring(contextString.indexOf(",readDataString: ") + 17, contextString.indexOf(",,Methods")).trim());
		repititionsGet(stepNum);

	}

	private void repititionsGet(int stepNum) {
		// 先找出拍照的位置
		int cameraStep = stepNum-1;
//		for (MethodBean bean : activityMethodBeans) {
//			if (bean.getRightSelect()) {
//				break;
//			}
//			cameraStep++;
//		}
		ArrayList<ArrayList<Integer>> ranges = new ArrayList<ArrayList<Integer>>();
		ArrayList<Integer> littleRange;
		int startStep = 0;
		for (MethodBean bean : activityMethodBeans) {
			if (!TextUtils.isEmpty(bean.getGoTo())) {
				littleRange = new ArrayList<Integer>();
				littleRange.add(startStep);
				littleRange.add(Integer.parseInt(bean.getGoTo())-1);
				ranges.add(littleRange);
			}
			startStep++;
		}

		// 判断有没有跳转
		if (ranges.size() == 0) {
			repititions = 1;
			return;
		} else {
			int sum = 1;
			for (ArrayList<Integer> little : ranges) {
				if (cameraStep <= little.get(0)) {
					if (cameraStep >= little.get(1)) {
						sum = sum * Integer.parseInt(activityMethodBeans.get(little.get(0)).getRepititions());
					}
				}
			}
			repititions = sum;
			return;
		}

	}

	@Override
	protected void dataView() {

	}

	private void getDefaultValue() {
		ctSettingBeans = ((ParameterSettingActivity) getActivity()).ctSettingBeans;
		// 先看activity有没有值。
		if (ctSettingBeans != null & ctSettingBeans.size() > 0) {

		} else {
			ctSettingBeans = new ArrayList<CtSettingBean>();
			for (int i = 0; i < activityLayoutTableBeans.size(); i++) {
				// if (defaultThresholdBoo) {
				// ctSettingBeans.add(new CtSettingBean(activityLayoutTableBeans
				// .get(i).getTarget(), "AUTO", "AUTO", "AUTO"));
				// } else {
				// if (defaultBaselineBoo) {
				// ctSettingBeans.add(new CtSettingBean(
				// activityLayoutTableBeans.get(i).getTarget(),
				// defaultThreshold, "AUTO", "AUTO"));
				// } else {
				// ctSettingBeans.add(new CtSettingBean(
				// activityLayoutTableBeans.get(i).getTarget(),
				// defaultThreshold, defaultStart, defaultEnd));
				// }
				// }
				ctSettingBeans.add(new CtSettingBean(activityLayoutTableBeans.get(i).getTarget(), defaultThreshold,
						defaultStart, defaultEnd));
			}
		}
		for (int i = 0; i < ctSettingBeans.size(); i++) {
			ctSettingBeans.get(i).setSelected(false);
		}

		ctSettingBeans.get(0).setSelected(true);

		// 获取

		if (contextString.toString().contains("ParameterSetting,,default,")) {

			contextString = new StringBuffer(contextString
					.substring(contextString.indexOf("ParameterSetting,,default,") + 26, contextString.length()));

			// default
			defaultThresholdBoo = Boolean.parseBoolean(contextString.substring(
					contextString.indexOf("defaultThresholdBoo:") + 20, contextString.indexOf(",defaultThreshold:")));
			defaultThreshold = contextString.subSequence(contextString.indexOf("defaultThreshold:") + 17,
					contextString.indexOf(",defaultBaselineBoo:")).toString().trim();
			defaultBaselineBoo = Boolean.parseBoolean(contextString.substring(
					contextString.indexOf("defaultBaselineBoo:") + 19, contextString.indexOf(",defaultStart:")));
			defaultStart = contextString
					.substring(contextString.indexOf("defaultStart:") + 13, contextString.indexOf(",defaultEnd:"))
					.toString().trim();
			defaultEnd = contextString.substring(contextString.indexOf("defaultEnd:") + 11,
					contextString.indexOf(",,ctSettingBeans"));

			// ctSettingBeans
			contextString = new StringBuffer(contextString.substring(contextString.indexOf(",,ctSettingBeans,") + 17,
					contextString.indexOf(",,advancedSettingBeans")));
			int index = 0;
			while (contextString.indexOf("targetName:", index) > 0) {

				String targetName = contextString.substring(contextString.indexOf("targetName:", index) + 11,
						contextString.indexOf(",threshold:", index));
				String threshold = contextString.substring(contextString.indexOf("threshold:", index) + 10,
						contextString.indexOf(",baselineStart:", index));
				String baselineStart = contextString.substring(contextString.indexOf("baselineStart:", index) + 14,
						contextString.indexOf(",baselineEnd:", index));
				String baselineEnd = contextString.substring(contextString.indexOf("baselineEnd:", index) + 12,
						contextString.indexOf(",useDaultSetting:", index));
				Boolean useDaultSetting = Boolean
						.parseBoolean(contextString.substring(contextString.indexOf("useDaultSetting:", index) + 16,
								contextString.indexOf(",isAutoThreshold:", index)));
				Boolean isAutoThreshold = Boolean
						.parseBoolean(contextString.substring(contextString.indexOf("isAutoThreshold:", index) + 16,
								contextString.indexOf(",isAutoBaseLine:", index)));

				int nextIndex = contextString.indexOf("isAutoBaseLine:", index) + 17;
				if (contextString.indexOf(",targetName:", nextIndex) < 0) {
					Boolean isAutoBaseLine = Boolean.parseBoolean(contextString
							.substring(contextString.indexOf("isAutoBaseLine:", index) + 15, contextString.length()));
					for (CtSettingBean bean : ctSettingBeans) {
						if (bean.getTargetName().equals(targetName)) {
							bean.setThreshold(threshold);
							bean.setBaselineStart(baselineStart);
							bean.setBaselineEnd(baselineEnd);
							bean.setUseDaultSetting(useDaultSetting);
							bean.setAutoThreshold(isAutoThreshold);
							bean.setAutoBaseLine(isAutoBaseLine);
						}
					}

				} else {
					Boolean isAutoBaseLine = Boolean
							.parseBoolean(contextString.substring(contextString.indexOf("isAutoBaseLine:", index) + 15,
									contextString.indexOf(",targetName:", nextIndex)));
					for (CtSettingBean bean : ctSettingBeans) {
						if (bean.getTargetName().equals(targetName)) {
							bean.setThreshold(threshold);
							bean.setBaselineStart(baselineStart);
							bean.setBaselineEnd(baselineEnd);
							bean.setUseDaultSetting(useDaultSetting);
							bean.setAutoThreshold(isAutoThreshold);
							bean.setAutoBaseLine(isAutoBaseLine);
						}
					}

				}

				index = contextString.indexOf("isAutoBaseLine:", index) + 19;

			}

		}

	}

	private class MyCtAdapter extends BaseAdapter {
		ArrayList<CtSettingBean> beans;

		public MyCtAdapter(ArrayList<CtSettingBean> ctSettingBeans) {
			super();
			this.beans = ctSettingBeans;
		}

		@Override
		public int getCount() {
			return beans != null ? beans.size() : 0;
		}

		@Override
		public Object getItem(int position) {

			return beans != null ? beans.get(position) : null;
		}

		@Override
		public long getItemId(int position) {

			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			MyViewHolder holder = null;
			if (convertView == null) {
				holder = new MyViewHolder();
				convertView = LayoutInflater.from(getActivity()).inflate(R.layout.item_ct_fragment, null);
				holder.traget_tv = (TextView) convertView.findViewById(R.id.traget_tv);
				holder.threshold_tv = (TextView) convertView.findViewById(R.id.threshold_tv);
				holder.start_tv = (TextView) convertView.findViewById(R.id.start_tv);
				holder.end_tv = (TextView) convertView.findViewById(R.id.end_tv);
				holder.line = (LinearLayout) convertView.findViewById(R.id.line);
				convertView.setTag(holder);
			} else {
				holder = (MyViewHolder) convertView.getTag();
			}

			holder.traget_tv.setText(beans.get(position).getTargetName());
			holder.threshold_tv.setText(beans.get(position).getThreshold());
			holder.start_tv.setText(beans.get(position).getBaselineStart());
			holder.end_tv.setText(beans.get(position).getBaselineEnd());
			if (beans.get(position).isSelected()) {
				holder.line.setBackground(getResources().getDrawable(R.color.selected_color));
				if (beans.get(position).isUseDaultSetting()) {
					default_check.setChecked(true);
				} else {
					default_check.setChecked(false);
				}

			} else {
				holder.line.setBackground(getResources().getDrawable(R.color.white));
			}

			if (beans.get(position).isUseDaultSetting()) {
				if (defaultThresholdBoo) {
					holder.threshold_tv.setText("AUTO");
					holder.start_tv.setText("AUTO");
					holder.end_tv.setText("AUTO");
				} else {
					if (defaultBaselineBoo) {
						holder.threshold_tv.setText(defaultThreshold);
						holder.start_tv.setText("AUTO");
						holder.end_tv.setText("AUTO");
					} else {
						holder.threshold_tv.setText(defaultThreshold);
						holder.start_tv.setText(defaultStart);
						holder.end_tv.setText(defaultEnd);
					}
				}
			} else {
				if (beans.get(position).isAutoThreshold()) {
					holder.threshold_tv.setText("AUTO");
					holder.start_tv.setText("AUTO");
					holder.end_tv.setText("AUTO");
				} else {
					if (beans.get(position).isAutoBaseLine()) {
						holder.threshold_tv.setText(beans.get(position).getThreshold());
						holder.start_tv.setText("AUTO");
						holder.end_tv.setText("AUTO");
					} else {
						holder.threshold_tv.setText(beans.get(position).getThreshold());
						holder.start_tv.setText(beans.get(position).getBaselineStart());
						holder.end_tv.setText(beans.get(position).getBaselineEnd());
					}
				}
			}

			holder.line.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					for (int i = 0; i < ctSettingBeans.size(); i++) {
						ctSettingBeans.get(i).setSelected(false);
					}
					ctSettingBeans.get(position).setSelected(true);

					if (ctSettingBeans.get(position).isUseDaultSetting()) {
						default_check.setChecked(true);
					} else {
						default_check.setChecked(false);
					}
					if (ctSettingBeans.get(position).isAutoThreshold()) {
						auto_threshold_check.setChecked(true);
					} else {
						auto_threshold_check.setChecked(false);
					}
					if (ctSettingBeans.get(position).isAutoBaseLine()) {
						auto_baseline_check.setChecked(true);
					} else {
						auto_baseline_check.setChecked(false);
					}
					default_check.setEnabled(true);

					// default_rel.setEnabled(true);
					auto_threshold_check.setEnabled(true);
					Threshold_edit.setEnabled(true);

					auto_baseline_check.setEnabled(true);
					baseline_start_lin.setEnabled(true);
					start_reduce.setEnabled(true);
					start_edit.setEnabled(true);
					start_add.setEnabled(true);
					end_reduce.setEnabled(true);
					end_edit.setEnabled(true);
					end_add.setEnabled(true);

					if (default_check.isChecked()) {

					}
					notifyDataSetChanged();

				}
			});
			return convertView;
		}
	}

	private class MyViewHolder {
		TextView traget_tv;
		TextView threshold_tv;
		TextView start_tv;
		TextView end_tv;
		LinearLayout line;
	}

	private void onClick() {
		start_add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					int d = Integer.parseInt(start_edit.getText() + "");
					d++;
					if (d < Integer.parseInt(end_edit.getText().toString())) {
						start_edit.setText(d + "");
					}

				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		});
		start_reduce.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					int d = Integer.parseInt(start_edit.getText() + "");
					d--;
					if (d >= 1) {
						start_edit.setText(d + "");
					}

				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		});
		end_add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					int d = Integer.parseInt(end_edit.getText() + "");
					d++;
					if (d <= repititions) {

						end_edit.setText(d + "");
					}
				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		});
		end_reduce.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					int d = Integer.parseInt(end_edit.getText() + "");
					d--;
					if (d > Integer.parseInt(start_edit.getText().toString())) {
						end_edit.setText(d + "");
					}

				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		});
	}

	private void popOnClick() {
		pop_start_add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					int d = Integer.parseInt(pop_start_edit.getText() + "");
					d++;
					if (d < Integer.parseInt(pop_end_edit.getText().toString())) {

						pop_start_edit.setText(d + "");
					}
				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		});
		pop_start_reduce.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					int d = Integer.parseInt(pop_start_edit.getText() + "");
					d--;

					if (d >= 1) {

						pop_start_edit.setText(d + "");
					}
				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		});
		pop_end_add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					int d = Integer.parseInt(pop_end_edit.getText() + "");
					d++;
					if (d <= repititions) {

						pop_end_edit.setText(d + "");
					}
				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		});
		pop_end_reduce.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					int d = Integer.parseInt(pop_end_edit.getText() + "");
					d--;
					if (d > Integer.parseInt(pop_start_edit.getText().toString())) {
						pop_end_edit.setText(d + "");
					}

				} catch (Exception e) {
					// TODO: handle exception

				}
			}
		});
	}

	private void checked(boolean isChecked) {
		if (isChecked) {
			// setNoFocus(default_rel);
			// setNoFocus(auto_threshold_check);
			// setNoFocus(Threshold_edit);
			// setNoFocus(auto_baseline_check);
			// setNoFocus(start_add);
			// setNoFocus(start_edit);
			// setNoFocus(start_reduce);
			// setNoFocus(end_add);
			// setNoFocus(end_edit);
			// setNoFocus(end_reduce);
		} else {
			// setOnFocus(default_rel);
			// setOnFocus(auto_threshold_check);
			// setOnFocus(Threshold_edit);
			// setOnFocus(auto_baseline_check);
			// setOnFocus(start_add);
			// setOnFocus(start_edit);
			// setOnFocus(start_reduce);
			// setOnFocus(end_add);
			// setOnFocus(end_edit);
			// setOnFocus(end_reduce);
		}

	}

	private void popChecked(boolean isChecked) {
		if (isChecked) {
			// setNoFocus(pop_rel);
			// setNoFocus(pop_baseline_check);
			// setNoFocus(pop_Threshold_tv);
			// setNoFocus(pop_Threshold_edit);

		} else {
			// setOnFocus(pop_baseline_check);
			// setOnFocus(pop_Threshold_tv);
			// setOnFocus(pop_Threshold_edit);

		}
	}

	private void setNoFocus(View view) {
		view.setAlpha(0.5f);
		view.setEnabled(false);
		view.setClickable(false);
	}

	private void setOnFocus(View view) {
		view.setAlpha(1.0f);
		view.setEnabled(true);
		view.setClickable(true);
	}

	// 定义一个回调接口
	public interface CallBackValue {
		public void SendMessageValue(ArrayList<CtSettingBean> ctSettingBeans, Boolean defaultBaselineBoo,
				String defaultStart, String defaultEnd, Boolean defaultThresholdBoo, String defaultThreshold);
	}
}
